package com.exposit.zbs.dao.impl;

import com.exposit.zbs.dao.IGrantApplicationDAO;
import com.exposit.zbs.dao.base.BaseDAO;
import com.exposit.zbs.entity.DateValueGraphicEntryEntity;
import com.exposit.zbs.entity.GrantApplicationEntity;
import com.exposit.zbs.entity.GrantAreaEntity;
import com.exposit.zbs.model.GrantApplication;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Repository
@Transactional
public class GrantApplicationDAO extends BaseDAO<GrantApplicationEntity, Long> implements IGrantApplicationDAO {

	//@Autowired
	//private SessionFactory sessionFactory;
	@SuppressWarnings("unchecked")
	public List<GrantApplication> listGrantApplication() {

		return null;
	}

	public List<GrantApplicationEntity> listGrantApplicationByArea(GrantAreaEntity grantArea) {

		return getSession().getNamedQuery("getGrantsByGrantArea").setString("grantAreaId", grantArea.getGrantAreaId().toString()).list();
	}

	@Override

	public List<GrantApplicationEntity> getEntitiesByStatus(String status) {

		return getSession().getNamedQuery("getGrantByStatus").setString("status", status).list();
	}

	@Override
	public Long getCountOfGrantRequests() {

		Long count = (Long) getSession().getNamedQuery("getCountOfRequestedGrantApplications").uniqueResult();
		return count;
	}

	@Override
	public List<DateValueGraphicEntryEntity> getMoneyToGrantsGraphicData() {

		List<Object[]> rows = getSession().createSQLQuery("CALL MoneyReport").list();
		List<DateValueGraphicEntryEntity> result = new ArrayList<DateValueGraphicEntryEntity>();
		DateValueGraphicEntryEntity temp = null;
		for (Object[] r : rows) {
			temp = new DateValueGraphicEntryEntity();
			temp.setDate((java.util.Date) r[1]);
			temp.setValue((Double) r[0]);
			result.add(temp);
		}

		return result;
	}
}
